package com.ruoyi.model.service.impl;

import java.util.List;


import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.model.domain.SModel;
import com.ruoyi.model.mapper.ModelMapper;
import com.ruoyi.model.domain.Model;
import com.ruoyi.model.service.IModelService;

/**
 * 模型Service业务层处理
 * 
 * @author cfy
 * @date 2025-02-14
 */
@Service
public class ModelServiceImpl implements IModelService 
{
    @Autowired
    private ModelMapper modelMapper;

    /**
     * 查询模型
     * 
     * @param id 模型主键
     * @return 模型
     */
    @Override
    public Model selectModelById(Long id)
    {
        return modelMapper.selectModelById(id);
    }

    /**
     * 查询模型列表
     * 
     * @param model 模型
     * @return 模型
     */
    @Override
    public List<Model> selectModelList(Model model)
    {
        return modelMapper.selectModelList(model);
    }

    /**
     * 新增模型
     * 
     * @param model 模型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertModel(Model model)
    {
        model.setCreateTime(DateUtils.getNowDate());
        int rows = modelMapper.insertModel(model);
        insertSModel(model);
        return rows;
    }

    /**
     * 修改模型
     * 
     * @param model 模型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateModel(Model model)
    {
        model.setUpdateTime(DateUtils.getNowDate());
        modelMapper.deleteSModelById(model.getId());
        insertSModel(model);
        return modelMapper.updateModel(model);
    }

    /**
     * 批量删除模型
     * 
     * @param ids 需要删除的模型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteModelByIds(Long[] ids)
    {
        modelMapper.deleteSModelByIds(ids);
        return modelMapper.deleteModelByIds(ids);
    }

    /**
     * 删除模型信息
     * 
     * @param id 模型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteModelById(Long id)
    {
        modelMapper.deleteSModelById(id);
        return modelMapper.deleteModelById(id);
    }

    /**
     * 新增源模型信息
     * 
     * @param model 模型对象
     */
    public void insertSModel(Model model)
    {
        List<SModel> sModelList = model.getSModelList();
        Long id = model.getId();
        if (StringUtils.isNotNull(sModelList))
        {
            List<SModel> list = new ArrayList<SModel>();
            for (SModel sModel : sModelList)
            {
                sModel.setId(id);
                list.add(sModel);
            }
            if (list.size() > 0)
            {
                modelMapper.batchSModel(list);
            }
        }
    }
}
